﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCF.Core.Forms.CustomControls.ComboBoxEditor
{
    /// <summary>
    /// 下拉编辑器
    /// </summary>
    /// <remarks>2014-3-7 by sutroon</remarks>
    public partial class ComboBoxEditor : UserControl
    {
        public Action<string, string> TextChangedHandler { get; set; }
        public Action<string, string> SaveDataHandler { get; set; }
        public Func<string, string> SelectedIndexChangedHandler { get; set; }
        public string[] Names { get { return cbbName.DataSource == null ? null : (string[])cbbName.DataSource; } set { cbbName.DataSource = value; } }
        public string Value { get { return tbValue.Text; } set { tbValue.Text = value; } }
        public ComboBoxEditor()
        {
            InitializeComponent();
            btnSave.Click += new EventHandler(btnSave_Click);
            cbbName.SelectedIndexChanged += new EventHandler(cbbName_SelectedIndexChanged);
            tbValue.TextChanged+=new EventHandler(tbValue_TextChanged);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveDataHandler != null) SaveDataHandler(cbbName.GetSelectedText(), tbValue.Text);
        }
        private void cbbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChangedHandler != null) tbValue.Text = SelectedIndexChangedHandler(cbbName.GetSelectedText());
        }
        private void tbValue_TextChanged(object sender, EventArgs e)
        {
            if (TextChangedHandler != null) TextChangedHandler(cbbName.GetSelectedText(), tbValue.Text);
        }
    }
}
